//
//  ZBBListenNewsViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBListenNewsViewController.h"
#import "TwoToOne.h"
#import "FitHeader.h"
#import "ZBBListenNewsTitleCell.h"
#import "ZBBContentListenNewsOrdinaryCell.h"
#import "ZBBContentListenNewsRecomendCell.h"
#import "TabBarController.h"
#import "ZBBProVinceCell.h"
#import "HeadView.h"
#import "TabBarController.h"
#import "LZBillboardViewController.h"
#import "Model.h"

@interface ZBBListenNewsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,ZBBCategoryContentsDelegate>
@property(nonatomic,strong)UICollectionView *titleCollectionView;
@property(nonatomic,strong)UICollectionView *contentCollectionView;
@property(nonatomic, strong)NSMutableArray *titleSourceArray;
@property(nonatomic, assign)NSInteger flag;
@property(nonatomic, strong)BaseButton *arrowButton;
@property(nonatomic, assign)NSInteger temp;
@property(nonatomic, strong)UICollectionView *categoryCollectionView;
@property(nonatomic, strong)NSArray *array;
@property(nonatomic, strong)NSDictionary *stateDictionary;
@property(nonatomic, strong)UILabel *transitionLabel;
@property(nonatomic, strong)BaseButton *stateButton;
@property(nonatomic,strong)NSString *stateString;
@property(nonatomic, assign)NSInteger flag1;
@property(nonatomic, assign)NSInteger temp1;
@end

@implementation ZBBListenNewsViewController
-(void)viewWillAppear:(BOOL)animated
{
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    [TabBarController shareInstance].myView.backgroundColor = [UIColor clearColor];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.84 green:0.23 blue:0 alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *titleFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    titleFlowLayout.minimumInteritemSpacing = 0;
    titleFlowLayout.minimumLineSpacing = 0;
     titleFlowLayout.sectionInset = UIEdgeInsetsMake(-10 * FITHeight, 0, 0, 0);
    titleFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _titleCollectionView.showsHorizontalScrollIndicator = NO;
    _titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 60 * FITWidth, 40 * FITHeight) collectionViewLayout:titleFlowLayout];
   [_titleCollectionView registerClass:[ZBBListenNewsTitleCell class] forCellWithReuseIdentifier:@"title"];
    _titleCollectionView.pagingEnabled = NO;
    _titleCollectionView.dataSource = self;
    _titleCollectionView.delegate = self;
    _titleCollectionView.layer.borderWidth = 0.5;
    _titleCollectionView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _titleCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_titleCollectionView];

    UICollectionViewFlowLayout *contentFlowlayout = [[UICollectionViewFlowLayout alloc] init];

    contentFlowlayout.sectionInset = UIEdgeInsetsMake(-10 * FITHeight, 0, 0, 0);
    contentFlowlayout.minimumInteritemSpacing = 0;
    contentFlowlayout.minimumLineSpacing = 0;
    contentFlowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 * FITHeight, self.view.frame.size.width, self.view.frame.size.height - 64 - 40 * FITHeight) collectionViewLayout:contentFlowlayout];
    [self.view addSubview:_contentCollectionView];
    _contentCollectionView.delegate = self;
    _contentCollectionView.dataSource = self;
    _contentCollectionView.pagingEnabled = YES;
    [_contentCollectionView registerClass:[ZBBContentListenNewsRecomendCell class] forCellWithReuseIdentifier:@"recomendContent"];
     [_contentCollectionView registerClass:[ZBBContentListenNewsOrdinaryCell class] forCellWithReuseIdentifier:@"ordinaryContent"];
      _contentCollectionView.backgroundColor = [UIColor grayColor];
    
    
    
    
    UICollectionViewFlowLayout *categoryFlowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    categoryFlowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    categoryFlowlayout.minimumInteritemSpacing = 0;
    categoryFlowlayout.minimumLineSpacing = 0;
    
    self.categoryCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -self.view.frame.size.height , self.view.frame.size.width, (self.view.frame.size.height - 64 - 40 * FITHeight)) collectionViewLayout:categoryFlowlayout];
    
    [self.view addSubview:_categoryCollectionView];
    _categoryCollectionView.delegate = self;
    _categoryCollectionView.dataSource = self;
    _categoryCollectionView.pagingEnabled = YES;
    _categoryCollectionView.backgroundColor = [UIColor whiteColor];
    
    [_categoryCollectionView registerClass:[ZBBProVinceCell class] forCellWithReuseIdentifier:@"category"];
   
    [_categoryCollectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headview"];
    
    [self.stateButton addTarget:self action:@selector(stateButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_categoryCollectionView addSubview:self.stateButton];
    
    
    [self.arrowButton setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
    [self.arrowButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.arrowButton];
    _categoryCollectionView.hidden = YES;
    
   
    _flag1 = 4;
    
}
-(void)passUrlString:(NSString *)urlString And:(BOOL)fromZBB

{
    LZBillboardViewController *bvc = [[LZBillboardViewController alloc]init];
    bvc.urlString = urlString;
    bvc.fromZBB = fromZBB;
    [self.navigationController pushViewController:bvc animated:YES];
}
-(BaseButton *)stateButton
{
    if (_stateButton == nil) {
        _stateButton = [BaseButton buttonWithType:UIButtonTypeSystem];
        _stateButton.frame = CGRectMake(147.5 * FITWidth, 450 * FITHeight, 80 * FITWidth, 80 * FITHeight);
        _stateButton.backgroundColor = [UIColor redColor];
        [_stateButton setTitle:@"完成" forState:UIControlStateNormal];
        _stateButton.layer.cornerRadius = 40 * FITHeight;
        [_stateButton setFontSize:25];
        [_stateButton setTintColor:[UIColor whiteColor]];
    }
    return _stateButton;
}
-(BaseButton*)arrowButton
{
    
    if (_arrowButton == nil) {
        
        _arrowButton = [BaseButton buttonWithType:UIButtonTypeSystem];
        _arrowButton.frame = CGRectMake(335 * FITWidth, 15 * FITHeight, 16 * FITWidth, 16 * FITHeight) ;
        
    }
    return _arrowButton;
}
-(void)stateButtonAction
{
    [UIView animateWithDuration:0.5 animations:^{
        [self.arrowButton setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
        [TabBarController shareInstance].zzzView.frame = CGRectMake(155 * FITWidth, -15 * FITHeight, 60 * FITWidth, 90 * FITHeight);
        _categoryCollectionView.frame = CGRectMake(0,  - self.view.frame.size.height, self.view.frame.size.width, (self.view.frame.size.height - 64 - 40 * FITHeight));
        self.transitionLabel.hidden = YES;
        
    }];
    _temp = 1;
    [_contentCollectionView reloadData];
}
-(void)buttonAction
{
    
    if (_temp) {
        [UIView animateWithDuration:0.5 animations:^{
                       [self.arrowButton setImage:[UIImage imageNamed:@"shang.png"] forState:UIControlStateNormal];
            _categoryCollectionView.hidden = NO;
            self.transitionLabel.hidden = NO;
            _categoryCollectionView.frame  =  CGRectMake(0, 50 * FITHeight , self.view.frame.size.width, self.view.frame.size.height);
            
            [TabBarController shareInstance].zzzView.frame = CGRectMake(155 * FITWidth, -15 * FITHeight + 65 * FITHeight , 60 * FITWidth, 90 * FITHeight);
                    }];
        
        _temp = 0;
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            
            [self.arrowButton setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
           
            [TabBarController shareInstance].zzzView.frame = CGRectMake(155 * FITWidth, -15 * FITHeight, 60 * FITWidth, 90 * FITHeight);

            _categoryCollectionView.frame = CGRectMake(0,  - self.view.frame.size.height, self.view.frame.size.width, (self.view.frame.size.height - 64 - 40 * FITHeight));
                     self.transitionLabel.hidden = YES;

        }];
        _temp = 1;
        
           }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{
    
    if (collectionView == _categoryCollectionView) {
        return CGSizeMake(self.view.frame.size.width, 30 * FITHeight);
    }
    else
    {
        return CGSizeMake(0, 0);
    }
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeadView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headview" forIndexPath:indexPath];
    
    if (collectionView == _categoryCollectionView) {
        
        if (indexPath.section == 0) {
            view.leftLabel.text = @"类别:";
        }
         else if(indexPath.section == 1)
         {
             view.leftLabel.text = @"排序:";
         }
        else
        {
            view.leftLabel.text = @"状态:";
        }
        return view;
    }
    else
    {
        return nil;
    }
    
    
    
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView == _categoryCollectionView) {
        return 3;
    }
    else
    {
        return 1;
    }
}
-(UILabel *)transitionLabel
{
    
    if (_transitionLabel == nil) {
        _transitionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 335 * FITWidth, 50 * FITHeight)];
        [self.view addSubview:_transitionLabel];
        _transitionLabel.text = @"请选择类别/筛选:";
        _transitionLabel.font = [UIFont systemFontOfSize: 13];
        _transitionLabel.textColor = [UIColor grayColor];
        _transitionLabel.backgroundColor = [UIColor whiteColor];
    }
    return _transitionLabel;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _titleCollectionView) {
        
        return _titleSourceArray.count;
    }
    else if (collectionView == _contentCollectionView)
    {
        
        return _titleSourceArray.count;
    }
    else
    {
        if (section == 0) {
            return _titleSourceArray.count;
        }
         else if (section == 1)
         {
             return 3;
         }
        else
        {
            
            return 0;
        }
    }
    
        
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        
          NSString *string =  [_titleSourceArray[indexPath.row] objectForKey:@"tname"];
        return CGSizeMake(string.length * 20 * FITWidth, 40 * FITHeight);
    }
     else if (collectionView == _contentCollectionView)
     {
         
         return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
         
     }
   else
   {
       
        return CGSizeMake(120 * FITWidth, 30 * FITHeight);

       
          }
    
    return CGSizeMake(0, 0);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        
       ZBBListenNewsTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"title" forIndexPath:indexPath];
        cell.title =  [_titleSourceArray[indexPath.row] objectForKey:@"tname"];
        
        if (_flag == indexPath.row) {
            cell.label.textColor = [UIColor redColor];
        }
        else
        {
            cell.label.textColor = [UIColor grayColor];
        }
        return cell;
    }
     else if (collectionView == _contentCollectionView)
     {
         ZBBContentListenNewsRecomendCell *recomendCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recomendContent" forIndexPath:indexPath];
         
         
         ZBBContentListenNewsOrdinaryCell *ordinaryCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ordinaryContent" forIndexPath:indexPath];
         
         if (indexPath.row == 0) {
             recomendCell.urlString = _categoryId;
             recomendCell.delegate = self;
             recomendCell.nav = self.navigationController;
                return recomendCell;
         }
         else
         {
             
             if (_temp == 0) {
                 ordinaryCell.string = @"hot";
             }
             else
             {
                 ordinaryCell.string = _stateString;
                             }
             ordinaryCell.nav = self.navigationController;
             ordinaryCell.dic = _titleSourceArray[indexPath.row];

             return ordinaryCell;
         }
         
     }
    
   else if (collectionView == _categoryCollectionView)
   {
       
       ZBBProVinceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"category" forIndexPath:indexPath];
       cell.label.textColor = [UIColor grayColor];
       if (_flag == indexPath.row && indexPath.section == 0) {
           cell.label.textColor = [UIColor redColor];
       }
       
       if (_flag1 == indexPath.row && indexPath.section == 1) {
             cell.label.textColor = [UIColor redColor];
       }

       if (indexPath.section == 0) {
            cell.label.text = [_titleSourceArray[indexPath.row] objectForKey:@"tname"];
       }
         else if(indexPath.section == 1)
         {
             
             cell.label.text = _array[indexPath.row];
             
         }
      
       return cell;
       
   }
    
    
    
    return nil;
}

-(void)getData
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"推荐",@"tname" ,nil];
    _titleSourceArray = [NSMutableArray arrayWithObject:dic];
    NSString *urlTitleString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%@&contentType=album&device=android",_categoryId];
       [TwoToOne getDataByString:urlTitleString BodyString:nil WithDataBlock:^(id data) {
           NSArray *array = [data objectForKey:@"list"];
           for (NSDictionary *dic in array) {
               
               [_titleSourceArray addObject:dic];
           }
           _stateDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"hot",@"最火",@"recent",@"最近更新",@"classic",@"经典",nil];
        
           _array = [_stateDictionary allKeys];
           [_contentCollectionView reloadData];
           [_titleCollectionView reloadData];
           [_categoryCollectionView reloadData];
       }];
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
              [_contentCollectionView setContentOffset:CGPointMake(self.view.frame.size.width * indexPath.row, 0) animated:YES];
           _flag = indexPath.row;
        CGFloat length = 0.0;
        for (NSInteger i = 0; i < indexPath.row; i++) {
            length =  [[_titleSourceArray[i] objectForKey:@"tname"] length] * 20 * FITWidth + length;
            
        }
    [_titleCollectionView  setContentOffset:CGPointMake(length , 0) animated:YES];
        [_titleCollectionView reloadData];
        
    }
    
    
    if (collectionView == _categoryCollectionView) {
       
        if (indexPath.section == 0) {
            [_contentCollectionView setContentOffset:CGPointMake(self.view.frame.size.width * indexPath.row, 0) animated:YES];
            _flag = indexPath.row;
            CGFloat length = 0.0;
            for (NSInteger i = 0; i < indexPath.row; i++) {
                length =  [[_titleSourceArray[i] objectForKey:@"tname"] length] * 20 + length;
                
            }
            
            _stateString = @"hot";
            if (_flag == 0) {
                _flag1 = 4;
            }
            [_titleCollectionView  setContentOffset:CGPointMake(length , 0) animated:YES];
            [_titleCollectionView reloadData];
            [_categoryCollectionView reloadData];
            
            
            
            
        }
         else
         {
             
             if (_flag != 0) {
                _flag1 = indexPath.row;
                 _stateString = [_stateDictionary objectForKey:_array[indexPath.row]];
                 [_categoryCollectionView reloadData];
               _temp = 1;
             }
             
         }
        
    }
    
    
    
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_contentCollectionView == scrollView) {
        _flag = scrollView.contentOffset.x / self.view.frame.size.width;
        
        
        CGFloat length = 0.0;
        for (NSInteger i = 0; i < _flag; i++) {
            length =  [[_titleSourceArray[i] objectForKey:@"tname"] length] * 20 + length;
            
        }
        [_titleCollectionView  setContentOffset:CGPointMake(length , 0) animated:YES];

     
          [_titleCollectionView reloadData];
       
      
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
